package ejb.entity;

import entity.Message;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class MessageFacade {

    private List<Message> messages;
    @PersistenceContext(unitName = "tryPostConstructApp-ejbPU")
    private EntityManager em;

    @PostConstruct
    public void init() {
        System.out.println("BEAN " + MessageFacade.class + " CONSTRUCTED");
    }

    @PreDestroy
    public void cleanup() {
        System.out.println("BEAN " + MessageFacade.class + " CLEANED UP");
    }

    public List<Message> getMessages() {
        Query getMessagesQuery = em.createNamedQuery("Message.findAll");
        messages = getMessagesQuery.getResultList();
        return messages;
    }
}
